package payroll.webapp

class EmployeeController extends AuthController {

    def add = {
		if (request.method == 'GET') {
			def employee = new Employee()
			def person	 = new PersonDetails()
			def salary	 = new BaseSalary()
			def contact  = new Contact()
			def permAddr = new Address()
			def presAddr = new Address()
			def bankDetails = new EmpBankDetails()
					
			[employee		 : employee,
			 person			 : person,
			 salary			 : salary,
			 contact		 : contact,
			 permAddr		 : permAddr,
			 presAddr		 : presAddr,
			 bankDetails	 : bankDetails,
			 departments	 : Department.list(),
			 designations	 : Designation.list(),
			 nationalities	 : Nationality.list(),
			 countries		 : Country.list(),
			 statusCodes	 : StatusCode.list(),
			 residenceTypes  : ResidentialTypes.list(),
			 costCenters     : EmployeeCostCenter.list()]
		}
		else {
			def employee = new Employee(params['employee'])
			def person   = new PersonDetails(params['person'])
			def salary	 = new BaseSalary(params['salary'])
			def contact  = new Contact(params['contact'])
			def permAddr = new Address(params['permAddr'])
			def presAddr = new Address(params['presAddr'])
			def bankDetails = new EmpBankDetails(params['bankDetails'])
			
			println("Inside ADD "+bankDetails);
			person.permanentAddress = permAddr
			person.contact  = contact
			employee.person = person
			employee.salary = salary
			employee.presentAddress = presAddr
			employee.bankDetails = bankDetails
			
			if (employee.save()) {
				redirect action:'list'
			}
			else {
				render 	view	:  'add', 
						model	:  [employee		: employee,
									person			: person,
									salary			: salary,
									contact			: contact,
									permAddr		: permAddr,
									presAddr		: presAddr,
									bankDetails		: bankDetails,
									departments		: Department.list(),
									designations	: Designation.list(),
									nationalities	: Nationality.list(),
									countries		: Country.list(),
									statusCodes		: StatusCode.list(),
									residenceTypes  : ResidentialTypes.list(),
									costCenters     : EmployeeCostCenter.list()]
			}
		}
	}
	
	def edit = {
		def employee = Employee.get(params.id)
		def person   = employee.person
		def salary	 = employee.salary
		def presAddr = employee.presentAddress
		def contact  = person.contact
		def permAddr = person.permanentAddress
		def bankDetails = employee.bankDetails
		
		def map =   [employee		 : employee,
					 person			 : person,
					 salary			 : salary,
					 contact		 : contact,
					 presAddr		 : presAddr,
					 permAddr		 : permAddr,
					 bankDetails	 : bankDetails,
					 departments	 : Department.list(),
					 designations	 : Designation.list(),
					 nationalities	 : Nationality.list(),
					 countries		 : Country.list(),
					 statusCodes	 : StatusCode.list(),
					 residenceTypes  : ResidentialTypes.list(),
					 costCenters     : EmployeeCostCenter.list()]
		
		if(request.method == 'GET') {
			render view:'add', model:map
		}
		else
		{
			employee.properties 	= params['employee']
			person.properties 		= params['person']
			salary.properties 		= params['salary']
			contact.properties 		= params['contact']
			presAddr.properties 	= params['presAddr']
			permAddr.properties 	= params['permAddr']
			bankDetails.properties   = params['bankDetails']
			
			if (employee.save()) {
				redirect(action:'list')
			}
			else {
				render view:'add', model:map
			}
		}
	}
	
	def list = {
		[employees: Employee.list()]
	}
	
	def delete = {
		def employee = Employee.get(params.id)
	
		if (employee){
			employee.delete()
			redirect(action:'list')
		}
	}
	
}